package net.yebaihe.wc;

import java.util.ArrayList;
import java.util.HashMap;

import net.yebaihe.wc.DataMan.MSGTYPE;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class Cities extends StateMachine{

	@Override
	protected String getTopbarTitle() {
		return "切换城市";
	}

	//TODO get cities from local db
	String idxBtnTitles[]={
			"北京",
			"上海",
			"南京",
			"深圳",
		};
	@Override
	protected void prepareClient(View client) {
		TextView city1=(TextView) client.findViewById(R.id.txtCity1);
		city1.setText(this.getWCAppContext().city);
		
		TextView city2=(TextView) client.findViewById(R.id.txtCity2);
		city2.setText(this.getWCAppContext().cellcity);
		city2.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View arg0) {
				Cities.this.getWCAppContext().city=getWCAppContext().cellcity;
				Pref.get(Cities.this).save("city", Cities.this.getWCAppContext().city);
				finish();
			}
			
		});
		
		ListView listview = (ListView) client.findViewById(R.id.listcities); 
		ArrayList<HashMap<String, Object>> meumList = new ArrayList<HashMap<String, Object>>(); 
		for(int i = 1;i < idxBtnTitles.length+1;i++) { 
			HashMap<String, Object> map = new HashMap<String, Object>(); 
			map.put("title", idxBtnTitles[i-1]); 
			meumList.add(map); 
		} 

		SimpleAdapter saMenuItem = new SimpleAdapter(this, 
				meumList, //数据源 
				R.layout.cities_item, //xml实现 
				new String[]{"title",}, //对应map的Key 
				new int[]{R.id.txtItem,});  //对应R的Id 

		//添加Item到网格中 
		listview.setAdapter(saMenuItem); 
		listview.setOnItemClickListener(new OnItemClickListener() { 
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
				Cities.this.getWCAppContext().city=idxBtnTitles[arg2];
				Pref.get(Cities.this).save("city", Cities.this.getWCAppContext().city);
				finish();
			}
		});
	}

	@Override
	public void HandleMessage(MSGTYPE msgtype, boolean ret,
			HashMap<String, String> params) {
		super.HandleMessage(msgtype, ret, params);
	}

	@Override
	protected int getLayoutRes() {
		return R.layout.cities;
	}

}
